package com.newhonor.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.newhonor.server.mapper.MenuRoleMapper;
import com.newhonor.server.pojo.MenuRole;
import com.newhonor.server.pojo.ResBean;
import com.newhonor.server.service.IMenuRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author newhonor
 * @Date 2021/2/22 22:01
 * @Version 1.0
 **/
@Service
public class MenuRoleIServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService{

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    /**
     * 更新角色菜单
     * @param rid
     * @param mids
     * @return
     */
    @Override
    @Transactional   //两边操作使用事务，抛出异常才rollback，自行捕获不会（下面的代码应该不对！！！）
    public ResBean updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMapper.delete(new QueryWrapper<MenuRole>().eq("rid", rid));
        if(null == mids || 0 == mids.length){
            return ResBean.success("更新成功");
        }
        Integer result = menuRoleMapper.insertRecord(rid,mids);
        if(result == mids.length){
            return ResBean.success("更新成功");
        }
        return ResBean.error("更新失败");
    }
}
